package com.whd.service.impl;

import com.whd.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author whd
 * @date 2023/07/01 11:12:13
 */
@Service
public class FileServiceImpl implements FileService {


        public String addPhoto(String path, MultipartFile photo) throws IOException {
            String fileName = photo.getOriginalFilename();
            //获取随机名字
            String newPhoto = UUID.randomUUID().toString();
            String newFile = newPhoto+fileName.substring(fileName.lastIndexOf("."));

            //给文件创建文件夹
            File file = new File(path);
            if (!file.exists()){
                file.mkdir();
            }
            String newPath = path + "/" + newFile;
            //将图片放到服务器中
            System.out.println(newPath+"*********************************");
            photo.transferTo(new File(newPath));
            //返回图片名字
            return newFile;
        }

        @Override
        public void removePhoto(String path, String photoFile) {
            File file = new File(path+ "/" + photoFile);
            System.out.println(path+ "/" + photoFile);
            file.delete();
        }
    }
